#include <google/protobuf/text_format.h>
#include <rocksdb/db.h>

#include <iostream>
#include <vector>

#include "raft/meta.h"
#include "raft/raft.pb.h"
#include "raft/raft_log.h"

void LogReplicationGenerate(const std::string log_path, std::string meta_path,
                            int log_num, int term) {
  rocksdb::DB* meta_db;
  rocksdb::Options meta_options;
  meta_options.create_if_missing = true;
  rocksdb::Status s = rocksdb::DB::Open(meta_options, meta_path, &meta_db);
  std::shared_ptr<rocksdb::DB> db_meta(meta_db);
  std::shared_ptr<libraft::Meta> meta(new libraft::Meta(db_meta));
  meta->Open();

  rocksdb::DB* log_db;
  rocksdb::Options log_options;
  log_options.create_if_missing = true;
  s = rocksdb::DB::Open(log_options, log_path, &log_db);
  std::shared_ptr<rocksdb::DB> db_log(log_db);
  std::shared_ptr<libraft::RaftLog> raft_log(new libraft::RaftLog(db_log));

  std::vector<const raft::Entry*> entries;
  for (int i = 0; i < log_num; i++) {
    raft::Entry entry;
    entry.set_term(term);
    entry.set_key(std::to_string(i));
    entry.set_value(std::to_string(i));
    entry.set_optype(raft::Entry_OpType_kWrite);
    entries.push_back(&entry);
  }

  raft_log->Append(entries);
  meta->SetCurrentTerm(term);
  meta->SetCommitIndex(term);
  meta->SetLastApplied(0);
}

int main() {
  LogReplicationGenerate("./log1", "./meta1", 10, 10);
  LogReplicationGenerate("./log2", "./meta2", 100, 10);
  LogReplicationGenerate("./log3", "./meta3", 10, 10);
  LogReplicationGenerate("./log4", "./meta4", 10, 10);
  LogReplicationGenerate("./log5", "./meta5", 10, 10);
  return 0;
}
